package com.sir.tao.carhire.util;


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author dyy
 * @data 2021/12/9 15:56
 * @description:
 */
public class DateUtil {
    public static Date getNextDayOfDay(Date d, int i) {
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(d);
        startTime.set(Calendar.HOUR_OF_DAY, 0);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.SECOND, 0);
        startTime.set(Calendar.MILLISECOND, 0);
        startTime.add(Calendar.DAY_OF_MONTH, i);
        return startTime.getTime();

    }

    public static Integer getDay(Date date){
        Calendar calendar = dataToCalendar(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
    public static Integer getMonth(Date date){
        Calendar calendar = dataToCalendar(date);
        return calendar.get(Calendar.MONTH);
    }
    public static Integer getYear(Date date){
        Calendar calendar = dataToCalendar(date);
        return calendar.get(Calendar.YEAR);
    }

    public static String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String getTime(Date currentTime, Date firstTime){
        //这样得到的差值是微秒级别
        long diff = currentTime.getTime() - firstTime.getTime();
        //当前系统时间转Calendar类型
        Calendar currentTimes =dataToCalendar(currentTime);
        //查询的数据时间转Calendar类型
        Calendar  firstTimes =dataToCalendar(firstTime);
        //获取年
        int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);
        int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
        int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            month -= 1;
            currentTimes.add(Calendar.MONTH, -1);
            //获取日
            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        if (month < 0) {
            //获取月
            month = (month + 12) % 12;
            year--;
        }
        long days = diff / (1000 * 60 * 60 * 24);
        //获取时
        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
        //获取分钟
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
        //获取秒
        long s=(diff/1000-days*24*60*60-hours*60*60-minutes*60);
        return ""+year+"年"+month+"月"+day+"天";
    }
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 获取今天的时间戳
     * @return 时间戳数组
     */
    public static long[] getTodayTimeFrame(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        long[] frames = new long[]{0, 0};
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 今日0时的时间戳
        frames[0] = calendar.getTimeInMillis();
        // 明日0时前的时间戳，86400000是24小时的毫秒数
        frames[1] = frames[0] + 86400000 - 1;
        return frames;
    }
    /**
     * endTime比startTime多的天数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 相差的天数
     */
    public static int differDay(Date startTime,Date endTime)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startTime);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endTime);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //同一年
        if(year1 != year2) {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++) {
                //闰年
                if(i%4==0 && i%100!=0 || i%400==0)
                { timeDistance += 366; }
                //不是闰年
                else
                { timeDistance += 365; }
            }
            return timeDistance + (day2-day1) ;
        }
        //不同年
        else{
            return day2-day1;
        }
    }
    /**
     * endDate比startDate多的天数
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 相差的月份
     */
    public static Integer getDifMonth(Date startDate, Date endDate){
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(startDate);
        end.setTime(endDate);
        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }

    public static Date getMonthFirstDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
    public static Date getYearFirstDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int  currentYear = calendar.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    public  static  Date getYearFirst( int  year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

}
